<?php
// +----------------------------------------------------------------------
// | OneThink [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.onethink.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: 麦当苗儿 <zuojiazi@vip.qq.com> <http://www.zjzit.cn>
// +----------------------------------------------------------------------

namespace Home\Controller;
use Vendor\Tianyi\Sms;
/**
 * 空模块，主要用于显示404页面，请不要删除
 */
class OnedayController extends HomeController{
	//没有任何方法，直接执行HomeController的_empty方法
	//请不要删除该控制器
    //验证身份证
    public function checkIdCard($idCard){
        if (strlen ( $idCard ) != 18) {
            $this->response(array('info'=>'身份证号码不正确'),'json',400);
        }
        return;
    }
    //验证手机号
    public function checkPhone($phone){
        if (strlen ( $phone ) != 11 || substr ( $phone, 0, 1 ) != '1') {
            $this->response(array('info'=>'手机号格式不正确'),'json',400);
        }
        return;
    }
    //$phone=null,$token=null
    public function checkToken($phone=null,$token=null){       
        Vendor('Yuntongxun.SendTemplateSMS');
        $sms = new \SMS();
        $tokenResult =$sms->randCodeCheck($phone,$token);
        
        switch($tokenResult){
            case 2:
                $this->response(array('info'=>'验证码错误'),'json',400);
                break;
            case 0:
                $this->response(array('info'=>'验证码过期'),'json',400);
                break;
            case -1:
                $this->response(array('info'=>'系统错误'),'json',400);
                break;
        }
        // $this->response(array('info'=>'验证码正确'),'json',400);
        return;
    }
	public function orderAdd(){
        if(IS_POST){
            $data = json_decode(file_get_contents('php://input'), true);
            $adult =$data['adultArr'];
            $myphone =$data['contact_num'];
            $token =$data['token'];
            $this->checkPhone($myphone);
            $this->checkToken($myphone,$token);
            if($adult){
                foreach ($adult as $key => $value) {
                    $this->checkIdCard($value['identity_card']);
                }
               
            }       

            if($data){
                $json['success'] = true;
                $form_data = $this->orderForm($data);
                if($form_data['success']){
                	$order_info = $form_data['info']; 
                }else{
                    $json['success'] = false;
                    $json['error'] = $form_data['error'];
                }

                if($json['success']){
                	$data = $this->calculateTotalPrice($order_info);
                	$order_info['total_price'] = $data['total_price'];
                    $order_info['contacts'] = null;
                    $order_info['contacts'] = $data['contacts'];
                }  

                if($json['success']){
                	$order_add = $this->addOrder($order_info);
                	if($order_add['success']){
                        $json['info'] = $order_add['info'];
                    }else{
                        $json['success'] = false;
                        $json['error'] = $order_add['error'];
                    }
                }
                if($json['success']){
                    $json['order_id'] = $order_add['order_id'];
                }

                $this->ajaxReturn($json);
            }

             
        }
    }

    protected function calculateTotalPrice($order_info){
    	$article_id = $order_info['article_id'];
        $contacts = $order_info['contacts'];

        $discount_data = $this->getDiscount();
        $contacts_num = count($contacts);

        $price = floatval($this->getPrice($article_id,$contacts_num));

        $price = $this->is_Holiday($order_info['tour_time'])?$price*1.2:$price;

        $total_price = 0;
        if($order_info['need_english'] == 2){
            $total+=300;
        }
        foreach ($contacts as $key => $value) {
            $contacts[$key]['price'] = $discount_data[$value['contact_type']]*$price;
             $total_price += $discount_data[$value['contact_type']]*$price;  
        }
        $data['total_price'] = $total_price;
        $data['contacts'] = $contacts;
        return $data;
    }

    protected function getPrice($article,$contacts_num){
        $articleshowModel = D('articleshow');
        $price = $articleshowModel->getPrice($article,$contacts_num);
        return $price;
    }

    protected function is_Holiday($date){
        $holidayModel = D('holiday');
        $holiday_data = $holidayModel->getHoliday();
        $is_holiday = 0;
       
        foreach ($holiday_data as $key => $value) {
           
            if($date>=$value['date_start']&&$date<=$value['date_end']){
               
                $is_holiday = 1; 
            }               
           
        }
        return $is_holiday;
    }

    protected function getDiscount(){
        $discountModel = D('discount');
        $discount_data = $discountModel->getDiscount();
        return $discount_data;
    }

    protected function orderForm($order_data){
    /*
    测试数据
                $time = mktime('0','0','0','10','1','2016');
            $data = array(
            'need_english'=>'2',
            'order_remark'=>'hello',
            'venue'=>'南京西路',
            'contact_num'=>'13636514842',
            'contact_person'=>'不知道 ',
            'tour_time'=>$time,
            'article_id'=>'1',
            'contacts'=> array(
                '0'=> array(
                    'contact_type' =>1,
                    'contact_name'=>'不知道',
                    ),
                ),
            );

    */
        $json['success'] = true;
        if($json['success']){
            if(!isset($order_data['need_english'])){
                $info['need_english'] = 1;
            }else{
            	$info['need_english'] = $order_data['need_english'];
            }
        } 

        if($json['success']){
            if(!isset($order_data['order_remark'])){
                $info['order_remark'] = null;
            }else{
            	$info['order_remark'] = $order_data['order_remark'];
            }
        } 

        if($json['success']){
            if(!isset($order_data['venue'])){
                $json['success'] = false;
                $json['error'] = '未填写集合地';
            }else{
            	$info['venue'] = $order_data['venue'];
            }
        }

        if($json['success']){
            if(!isset($order_data['contact_num'])){
                $json['success'] = false;
                $json['error'] = '未填写联系人手机号';
            }else{
                $info['contact_num'] = $order_data['contact_num'];
            }
        }

        if($json['success']){
            if(!isset($order_data['contact_person'])){
                $json['success'] = false;
                $json['error'] = '未填写联系人姓名';
            }else{
                $info['contact_person'] = $order_data['contact_person'];
            }
        }

        if($json['success']){
            if(!isset($order_data['tour_time'])){
                $json['success'] = false;
                $json['error'] = '未填写出行时间';
            }else{
            	$info['tour_time'] = $order_data['tour_time'];
            }
        }

        if($json['success']){
            if(!isset($order_data['article_id'])){
                $json['success'] = false;
                $json['error'] = '未填写一日游项目';
            }else{
            	$info['article_id'] = $order_data['article_id'];
            }
        }  

        if($json['success']){
            if(isset($order_data['contacts'])){
                $contacts = $order_data['contacts'];
                $contact_adult = 0;//是否有一个成年人
                $contact_child = 0; //是否有联系人 
                foreach ($contacts as $key => $value) {

                    if($value['contact_type'] == 1){
                        $contact_adult++;
                    }else{
                        $contact_child++;
                    }
                    $save_data[$key]['contact_type'] = $value['contact_type']; 

                    if(isset($value['birth'])){
                        $save_data[$key]['birth'] = $value['birth'];
                    }else{
                        $save_data[$key]['birth'] = ''; 
                    }

                    if(isset($value['birthday'])){
                        $save_data[$key]['birthday'] = $value['birthday'];
                    }else{
                        $save_data[$key]['birthday'] = ''; 
                    }

                    if(isset($value['sex'])){
                        $save_data[$key]['sex'] = $value['sex'];
                    }else{
                        $save_data[$key]['sex'] = 'male'; 
                    }

                    if(isset($value['identity_card'])){
                        $save_data[$key]['identity_card'] = $value['identity_card'];
                    }else{
                        $save_data[$key]['identity_card'] = '';
                    }

                    if($value['contact_name'] == null){
                    	$json['success'] = false;
                    	$json['error'] = '出行人信息不完整';
                    }else{
                        $save_data[$key]['contact_name'] = $value['contact_name'];
                    }


                }
                if($json['success']){
                    $json['project'] = 1;
                    $json['info'] = $info;
                }
                if($json['success']){
                    if($json['success']){
                        if($contact_adult<$contact_child){
                        $this->response(array('info'=>'小孩必须有大人陪护'),'json',400);
                        }else{
                            $json['info']['contacts'] = $save_data;
                        } 
                    }
                    
                }
                
            }
        } 


        return $json;
        
    }

    /**
     * 手机号码: 
     * 13[0-9], 14[5,7], 15[0, 1, 2, 3, 5, 6, 7, 8, 9], 17[6, 7, 8], 18[0-9], 170[0-9]
     * 移动号段: 134,135,136,137,138,139,150,151,152,157,158,159,182,183,184,187,188,147,178,1705
     * 联通号段: 130,131,132,155,156,185,186,145,176,1709
     * 电信号段: 133,153,180,181,189,177,1700
     */
    //NSString *MOBILE = @"^1(3[0-9]|4[57]|5[0-35-9]|8[0-9]|70)\\d{8}$";
    //project 1一日游，2迪士尼
    protected function addOrder($order_info){
        $json['success'] = true;

        $order['is_cancel'] = 1;

        $order['uid'] = $this->login();

        $order['date_added'] = time();

        $order['order_type'] = 1;

        $order['status'] = 1;//(待付款)

        $order['project'] = $order_info['project'];

        $order['article_id'] = $order_info['article_id'];

        $order['order_price'] = $order_info['total_price'];

        $info['venue'] = $order_info['venue'];

        $info['contact_person'] = $order_info['contact_person'];

        $info['contact_num'] = $order_info['contact_num'];

        $info['tour_time'] = $order_info['tour_time'];

        $info['need_english'] = $order_info['need_english'];

        $contacts = $order_info['contacts'];

        if($json['success']){
            $orderModel = D('order');
            $order_id = $orderModel->addOrder($order);
            if(!$order_id){
                $json['success'] = false;
                $json['error'] = '添加订单出错';
            }
        }
        
        if($json['success']){
            $info['order_id'] = $order_id;
            $onedayModel = D('onedaydetail');
            $oneday_data = $onedayModel->addOnedayDetail($info);
            if(!$oneday_data){
                $json['success'] = false;
                $json['error'] = '添加订单详情出错';
            }
        }

        if($json['success']){
            $info['order_id'] = $order_id;
            $contactsModel = D('contacts');
            $contact_data = $contactsModel->addContacts($contacts,$order_id);
            if(!$contact_data){
                $json['success'] = false;
                $json['error'] = '添加联系人出错';
            }
        }
        if($json['success']){
           $json['order_id'] = $order_id;
           $json['info'] = '添加成功'; 
        }
        
        return $json;
    }
}
